home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / src / timer.cc < prev    next >
C/C++ Source or Header  |  1989-08-18  |  2KB  |  143 lines

  1. /*
  2.  *    
  3.  *    Written by
  4.  *
  5.  *    Bruce Eckel
  6.  *    School of Oceanography; WB-10
  7.  *    University of Washington
  8.  *    Seattle, WA 98195
  9.  *
  10.  *    @(#)timer.cc    2.1    8/18/89
  11.  */
  12.  
  13. #include "rw/Timer.h"
  14.  
  15. int    strlen(const char*);
  16. char*    strcpy(char*, const char*);
  17.  
  18. extern int getrusage(int, struct rusage*);
  19.  
  20. void
  21. Timer::timer_(float *seconds)
  22. {
  23.   struct rusage use;
  24.   double usertime, systime;
  25.  
  26.   getrusage(RUSAGE_SELF, &use);
  27.  
  28.   usertime = (double)use.ru_utime.tv_sec +
  29.     ((double)use.ru_utime.tv_usec)/1.0e6;
  30.  
  31.   systime = (double)use.ru_stime.tv_sec +
  32.     ((double)use.ru_stime.tv_usec)/1.0e6;
  33.  
  34.   *seconds = (float)(usertime + systime);
  35.  
  36.   return;
  37. }
  38.  
  39.  
  40.  Timer::Timer()
  41. {
  42.     current = head = new MarkPoint;
  43.     head->next = 0;
  44. }
  45.  
  46.  
  47. /* 
  48. -*++ Timer::~Timer(): 
  49. ** 
  50. ** (*++ history: 
  51. **     27 Apr 88    Bruce Eckel    Creation date
  52. ** ++*)
  53. ** 
  54. ** (*++ detailed: 
  55. ** ++*)
  56. */
  57.  
  58. void Timer::~Timer()
  59. {
  60.     MarkPoint * ptr = head;
  61.     MarkPoint * killptr;
  62.     while (ptr) {
  63.     killptr = ptr;
  64.     if (ptr->next) 
  65.         ptr = ptr->next;
  66.     else ptr = 0;
  67.     delete killptr->msg;
  68.     delete killptr;
  69.     }
  70. }
  71.     
  72.  
  73. /* 
  74. -*++ Timer::mark(): mark timing information at this point in the program
  75. ** 
  76. ** (*++ history: 
  77. **     27 Apr 88    Bruce Eckel    Creation date
  78. ** ++*)
  79. ** 
  80. ** (*++ detailed: 
  81. ** ++*)
  82. */
  83.  
  84. void Timer::mark()
  85. {
  86.     timer_(&(current->seconds));
  87.     current->msg = new char[ strlen("Marker ") + 1 ];
  88.     strcpy(current->msg,"Marker ");
  89.     current->next = new MarkPoint;
  90.     current = current->next;
  91.     current->next = (MarkPoint *)0; /* denote end of list */
  92.     current->seconds = 0;
  93. }
  94.  
  95.  
  96. /* 
  97. -*++ Timer::mark(): place a marker with a message
  98. ** 
  99. ** (*++ history: 
  100. **     27 Apr 88    Bruce Eckel    Creation date
  101. ** ++*)
  102. ** 
  103. ** (*++ detailed: 
  104. ** ++*)
  105. */
  106.  
  107. void Timer::mark(char * message)
  108. {
  109.     timer_(&(current->seconds));
  110.     current->msg = new char[ strlen(message) + 1 ];
  111.     strcpy(current->msg,message);
  112.     current->next = new MarkPoint;
  113.     current = current->next;
  114.     current->next = (MarkPoint *)0; /* denote end of list */
  115.     current->seconds = 0;
  116. }
  117.  
  118. /* 
  119. -*++ operator<<(): how to output timing information via streams
  120. ** 
  121. ** (*++ history: 
  122. **     27 Apr 88    Bruce Eckel    Creation date
  123. ** ++*)
  124. ** 
  125. ** (*++ detailed: 
  126. ** ++*)
  127. */
  128.  
  129. ostream& operator<<(ostream & s, Timer & t)
  130. {
  131.     int i = 1;
  132.     MarkPoint * ptr = t.head;
  133.     while (ptr->next) {
  134.     s << "Marker " << i << ": " << ptr->msg << ": time = ";
  135.     s << ptr-> seconds << "\n";
  136.     if (ptr->next) ptr = ptr->next;
  137.     else break;
  138.     i++;
  139.     } 
  140.     return s;
  141. }
  142.  
  143.